/*
 * TestGenerator.h
 *
 *  Created on: 2012-04-22
 *      Author: mm
 *
 *      Main class of the program. This class implements a simple macrogenerator that
 *      generates tests for parsers.
 *
 *      Implements singleton pattern.
 */

#ifndef TESTGENERATOR_H_
#define TESTGENERATOR_H_

#include <vector>
#include <string>
#include <cstdlib>
#include <stack>

class TestGenerator
{
public:
	typedef std::vector<std::string> Text;
	typedef std::vector<Text> Texts;
	typedef std::stack<std::string> TextStack;

	/**
	 *  Singleton class - new instances of it cannot be created
	 *  and the only instance can be obtained via the getInstance() method.
	 */
	static TestGenerator* getInstance()
	{
		if(!instance_)
			instance_ = new TestGenerator();
		return instance_;
	}

	/**
	 *  Registers a parsed line of input text in the generator.
	 */
	void addText(const Text& text);

	/**
	 *  Generates the output.
	 */
	void generate();

	/**
	 * 	Builds a text table suitable for generation out of the parsed lines.
	 */
	void buildTextTable();

	/**
	 *  Finds a definition by the given name and returns it as a Text.
	 *  If no definition is found, a Text consisting of the given string is returned.
	 */
	Text expandDefinition(std::string s);

	/**
	 *	Prints all registered texts - used for test reasons only.
	 */
	void printTexts();

	/**
	 *  Recursively prints the output lines.
	 *  start is the index of the next status (textTable cell) to print, position is
	 *  the index of the cell's next element.
	 */
	void nextText(int start, int element);

private:
	TestGenerator() {}
	TestGenerator(const TestGenerator&) {}
	virtual ~TestGenerator() {}

	Texts inputTexts_;	// parsed texts from input file
	Texts textTable_;	// data structure suitable for result generation
	TextStack stack_;	// holds results
	Text result_; 		// holds result
	int status_;
	int elementStatus_;
	static TestGenerator* instance_;
};

#endif /* TESTGENERATOR_H_ */
